package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;


    public BusinessDataVO getBusinessData() {
        // 获取当天时间范围
        LocalDateTime min = LocalDate.now().atStartOfDay();
        LocalDateTime max = LocalDate.now().atTime(LocalTime.MAX);

        // 今日新增用户数
        Integer newUsers = userMapper.countByTime(min, max);

        // 有效订单数（已完成订单）
        Integer validOrderCount = countByTimeAndStatus(min, max, Orders.COMPLETED);

        // 总订单数
        Integer totalOrderCount = orderMapper.countAll(min, max);

        // 订单完成率（避免除零错误）
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != null && totalOrderCount != 0) {
            orderCompletionRate = (double) validOrderCount / totalOrderCount;
        }

        // 营业额（确保非空）
        Double turnover = orderMapper.sumAmount(min, max, Orders.COMPLETED);
        if (turnover == null) {
            turnover = 0.0;
        }

        // 平均客单价（避免除零错误）
        Double unitPrice = 0.0;
        if (totalOrderCount != null && totalOrderCount != 0 && turnover != 0) {
            unitPrice = turnover / totalOrderCount;
        }

        // 构建返回对象
        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setTurnover(turnover);
        businessDataVO.setValidOrderCount(validOrderCount);
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setNewUsers(newUsers);

        return businessDataVO;
    }

    private Integer countByTimeAndStatus(LocalDateTime min, LocalDateTime max, Integer status) {
        return orderMapper.countByTimeAndStatus(min, max, status);

    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = getByStatus(StatusConstant.ENABLE);
        Integer discontinued = getByStatus(StatusConstant.DISABLE);
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(sold);
        setmealOverViewVO.setDiscontinued(discontinued);
        return setmealOverViewVO;
    }

    @Override
    public DishOverViewVO getOverviewDishs() {
        Integer sold = dishCountByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishCountByStatus(StatusConstant.DISABLE);
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(sold);
        dishOverViewVO.setDiscontinued(discontinued);
        return dishOverViewVO;
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {

        LocalDateTime min = LocalDate.now().atStartOfDay();
        LocalDateTime max = LocalDate.now().atTime(LocalTime.MAX);

        Integer toBeConfirmed = countByStatus(Orders.TO_BE_CONFIRMED, min, max);
        Integer confirmed = countByStatus(Orders.CONFIRMED, min, max);
        Integer completed = countByStatus(Orders.COMPLETED, min, max);
        Integer cancelled = countByStatus(Orders.CANCELLED, min, max);

        Integer allOrders = orderMapper.countAll(min, max);

        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setWaitingOrders(toBeConfirmed);
        orderOverViewVO.setDeliveredOrders(confirmed);
        orderOverViewVO.setCompletedOrders(completed);
        orderOverViewVO.setCancelledOrders(cancelled);
        orderOverViewVO.setAllOrders(allOrders);

        return orderOverViewVO;


    }

    private Integer countByStatus(Integer status, LocalDateTime min, LocalDateTime max) {
        return orderMapper.orderCountByStatus(status, min, max);
    }

    private Integer dishCountByStatus(Integer status) {
        return dishMapper.countByStatus(status);
    }

    private Integer getByStatus(Integer status) {
        return setmealMapper.countByStatus(status);
    }
}
